package com.wafersystems.cloud.service.impl.noticeservice;

import java.util.Iterator;

import javax.websocket.Session;

import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.wafersystems.cloud.service.InterfaceMsgNotice;
import com.wafersystems.cloud.web.websocket.SocketHandler;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WebsocketNotice implements InterfaceMsgNotice {

  @Override
  public void sendMsg(String msg) {
    Iterator<WebSocketSession> entries = SocketHandler.websocketConnects.iterator();
    while (entries.hasNext()) {
      WebSocketSession session = entries.next();
      try {
        session.sendMessage(new TextMessage(msg));
      } catch (Exception e) {
        log.error(e.getMessage(), e);
        SocketHandler.websocketConnects.remove(session);
      }
    }
  }

}
